package org.com.ashen.web.exceptionhandler;

import com.alibaba.fastjson.JSON;

import org.com.ashen.common.common.HttpResult;
import org.com.ashen.common.exception.BusinessException;
import org.com.ashen.common.exception.ErrorEnum;
import org.com.ashen.common.exception.SystemException;
import org.com.ashen.service.datasourceswitch.DataSourceContext;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * Created by yizhenn on 16-12-10.
 */
public class DefaultExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        if (e instanceof BusinessException) {
            System.out.println("this is a businessException");
            BusinessException businessException = (BusinessException) e;
            putErrorEnumToResponse(businessException.getErrorEnum(), httpServletResponse);
        } else if (e instanceof SystemException) {
            System.out.println("this is a systemException");
            SystemException systemException = (SystemException) e;
            putErrorEnumToResponse(systemException.getErrorEnum(), httpServletResponse);
        } else {
            e.printStackTrace();
            System.out.println("this is a defaultException");
            System.out.println(e.getMessage()+"errormessage");
            putExceptionToResponse(e.getMessage(), httpServletResponse);
        }
        return null;
    }

    private static void putErrorEnumToResponse(ErrorEnum errorEnum, HttpServletResponse response) {
        String result = JSON.toJSONString(HttpResult.failedResult(errorEnum));
        response.setContentType("application/json;charset=utf-8");
        DataSourceContext.clearDataSource();
        try {
        	response.getWriter().print(result);
        	response.getWriter().flush();
            if (response.getWriter() != null)
            	response.getWriter().close();
        } catch (IOException e) {
            System.out.println("put error msg to response exception");
            e.printStackTrace();
        }
    }
    
    
    private static void putExceptionToResponse(String excptionStr, HttpServletResponse response) {
        String result = JSON.toJSONString(HttpResult.failedResult(excptionStr));
        response.setContentType("application/json;charset=utf-8");
        DataSourceContext.clearDataSource();
        try {
        	response.getWriter().print(result);
        	response.getWriter().flush();
            if (response.getWriter() != null)
            	response.getWriter().close();
        } catch (IOException e) {
            System.out.println("put error msg to response exception");
            e.printStackTrace();
        }
    }

}
